package edu.xlh.travel.dao;

import edu.xlh.travel.pojo.Hotel_orders;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

public interface ForeHotelOrderDao extends JpaRepository<Hotel_orders,String>, JpaSpecificationExecutor<Hotel_orders> {
    List<Hotel_orders> findByUserIdOrderByModifyTimeDesc(String id);

    @Modifying
    @Query(value = "update t_hotel_orders set status =:#{#hotel_orders.status} where id =:#{#hotel_orders.id}",nativeQuery = true )
    void updateHotel_ordersById(@Param("hotel_orders") Hotel_orders hotel_orders);

    @Modifying
    @Query(value = "update t_hotel_orders set status =:#{#hotel_orders.status} , paytime=:#{#hotel_orders.paytime} , modify_time=:#{#hotel_orders.modifyTime} where id =:#{#hotel_orders.id}",nativeQuery = true )
    void updateHotelOrderByEntitiy(Hotel_orders hotel_orders);


    @Modifying
    @Query(value = "update Hotel_orders set status =?1 where id =?2")
    void updateStatusByHotelOrderId(@Param("status") String status , @Param("id") String id);

    @Modifying
    @Query(value = "update t_hotel set stock =?2 " +
            "where id = (" +
            "select a.scenic_id " +
            "from (" +
            "select o.scenic_id " +
            "from t_hotel_orders o " +
            "join t_hotel h " +
            "on o.scenic_id = h.id where o.id =?1) a )", nativeQuery=true)
    void updateStockByHotelOrderId(String orderId, int stocks);

    @Transactional
    @Modifying
    @Query(value = "update Hotel_orders set remarks =?2 where id =?1")
    void saveRemarks(@Param("orderId")String orderId, @Param("remarks")String remarks);

    int countByUserId(String userId);

    @Modifying
    @Query(value = "update Hotel_orders set status =?2 , modifyTime =?3 where id =?1")
    void updateHotelOrderModifyTimeById(@Param("orderId")String orderId, @Param("status")String status ,  @Param("modifyTime") Date modifyTime);

    @Modifying
    @Query(value = "update t_hotel_orders set head_order_id =:#{#hotel_orders.headOrderId} , tail_order_id=:#{#hotel_orders.tailOrderId}  where id =:#{#hotel_orders.id}",nativeQuery = true )
    void updateHeadAndTailByEntity(Hotel_orders hotel_orders);
}
